{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div class="layui-col-lg2 layui-col-md2 layui-col-sm2 layui-col-xl2 layui-col-sm2 layui-col-xs2 layui-padding-2">
    <h3>分类</h3>
    <div id="ID-tree-demo-showLine"></div>
</div>
<div class="layui-col-lg10  layui-col-md10  layui-col-xl10 layui-col-sm10  layui-col-sm10  layui-col-xs10 ">
<form class="layui-form gg-form-bar border-t border-x">
    
    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="name" placeholder="名称" class="layui-input" autocomplete="off" />
        <input type="hidden" name="type" id="type" value="0">
    </div>
    

    <div class="layui-input-inline" style="width:300px;">
        <select name="status" >
            <option value="">所有状态</option>
            {foreach ['','启用','禁用']  as $key=>  $v}
            <option value="{$key}">{$v}</option>
            {/foreach}
        </select>
    </div>

    <div class="layui-input-inline" style="width:150px;">
        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform" id="webf"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
        <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
    </div>
</form>
<div class="p-3">
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
<!--  	<button class="layui-btn layui-btn-sm addNew" type="button" lay-event="add">+ 添加模板</button>-->
  	<button class="layui-btn layui-btn-sm layui-btn-danger del" lay-event="del" type="button">删除</button>
  </div>
</script>
<script type="text/html" id="actionBar">
    {{# if(d.status == 1 ){ }}
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="forbid">
        <i class="fa fa-minus-square-o" aria-hidden="true"></i>禁用
    </a>
    {{# } }}

    {{# if(d.status == 2 ){ }}
    <a class="layui-btn layui-btn-xs dx_list_bottom" lay-event="resume">
        <i class="fa fa-check-square-o" aria-hidden="true"></i>启用
    </a>
    {{# } }}
    <button class="layui-btn layui-btn-normal  layui-btn-xs" lay-event="edit">编辑</button>
        <a href="javascript:" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="design">设计</a>
</script>
</div>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
	<script>
	const moduleInit = ['tool'];
	function gouguInit() {
		var table = layui.table, tool = layui.tool, form = layui.form,laydate=layui.laydate;
        var upload=layui.upload;
		var tableIns = table.render({
			elem: '#test'
			,toolbar: '#toolbarDemo'
			,defaultToolbar:  ['filter',  'exports']
			,title:'模板档案'
			,url: "{:url('index')}"
			,page: true
			,cellMinWidth: 80
			,cols: [[
                {type: 'checkbox', fixed: 'left' },
                // {fixed: 'left',field: 'id',title: 'ID' ,unresize: true,hide:true},
					{field:'id',width:80, title: 'ID号', align:'center'}
					// ,{field:'pCode',title: '客户编码'}
					,{field:'name',title: '模板名称'}

					,{field:'status', title: '状态',width:80,align:'center',templet: function(d){
						var html1='<span class="green">正常</span>';
						var html2='<span class="yellow">禁用</span>';
						if(d.status==1){
							return html1;
						}
						else{
							return html2;
						}
					}}
					,{title: '操作',fixed: 'right',width: 200, align:'center', toolbar: '#actionBar'}
				]]
			});
        //监听搜索提交
        form.on('submit(webform)', function (data) {
            console.log('data',data)
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });
			table.on('tool(test)',function (obj) {
				if(obj.event === 'edit'){
                    tool.side("{:url('add')}?id=" + obj.data.id);
				}
				if(obj.event === 'forbid'){
					layer.confirm('确定要禁用该数据吗?', {icon: 3, title:'提示'}, function(index){
						let callback = function (e) {
							layer.msg(e.msg);
							if (e.code == 0) {
                                tableIns.reload();
							}
						}
						tool.post("{:url('doCheck')}", { id: obj.data.id,status: 2}, callback);
						layer.close(index);
					});
				}
				if(obj.event === 'resume'){
					layer.confirm('确定要启用该数据吗?', {icon: 3, title:'提示'}, function(index){
						let callback = function (e) {
							layer.msg(e.msg);
							if (e.code == 0) {
                                tableIns.reload();
							}
						}
						tool.post("{:url('doCheck')}", { id: obj.data.id,status: 1}, callback);
						layer.close(index);
					});
				}
                if (obj.event === 'del') {
                    layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                        let callback = function (e) {
                            layer.msg(e.msg);
                            if (e.code == 0) {
                                obj.del();
                            }
                        }
                        tool.delete("{:url('delete')}", {id: obj.data.id}, callback);
                        layer.close(index);
                    });
                }
                if (obj.event === 'design') {
                   window.open("{:url('design')}?id="+obj.data.id)
                }
			});
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch(obj.event){
                case 'add':
                    tool.side("{:url('add')}?");
                    break;
                case 'del':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                            let callback = function (e) {
                                layer.msg(e.msg);
                                if (e.code == 0) {
                                    tableIns.reload();
                                }
                            }
                            tool.delete("{:url('delete')}", {id: ids.join(',')}, callback);
                            layer.close(index);
                        });

                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'import':
                    var loadingIndex='';
                    upload.render({
                        elem: '#import'
                        ,url: "{:url('importData')}"
                        ,accept: 'file'
                        ,exts: 'xlsx',
                        before:function (res) {
                            loadingIndex=layer.load(1,{
                                shade: [0.5,'#a09b9b']
                            });
                        }
                        ,done: function(res){
                            layer.msg(res.msg)
                            layer.close(loadingIndex)
                            setTimeout(function () {
                                location.reload()
                            },2000)
                        },
                        error(e){
                            layer.msg('网络异常请稍后再试')
                            layer.close(loadingIndex)
                        }
                    });
                    break;

            };
        })
			// $('body').on('click','.addNew',function(){
            //     tool.side("{:url('add')}?");
			// });
        var tree = layui.tree;
        $.ajax({
            url: "/home/api/getPrintType",
            type:'post',
            success:function(res){
                //仅节点左侧图标控制收缩
                tree.render({
                    elem: '#ID-tree-demo-showLine',
                    data: res.data,
                    onlyIconControl: true,  //是否仅允许节点左侧图标控制展开收缩
                    click: function(obj){
                        console.log('obj',obj)
                        //layer.msg(JSON.stringify(obj.data));
                        let type_id=obj.data.id

                        $("#type").val(type_id)
                        $("#webf").click()

                        layui.form.render('select');
                    }
                });
            }
        })

        laydate.render({
            elem: '#time1'
            ,type:'month'
        });
	}
	</script>
{/block}
<!-- /脚本 -->